package com.marvels.dubbo.coreservice.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.dragon.app.ykd.constant.ApplyNode;
import com.dragon.app.ykd.service.AcLoanApplyNodeService;
import com.dragon.loan.entity.AcLoanApplyInfo;
import com.dragon.loan.entity.AcLoanApplyLinkman;
import com.dragon.loan.entity.AcUserBank;
import com.dragon.loan.entity.McamAcLoanApplyBdInfo;
import com.dragon.loan.entity.McamAcLoanApplyImageInfo;
import com.dragon.loan.entity.McamLoanApplyInfo;
import com.dragon.loan.service.AcLoanApplyInfoExtDragonService;
import com.dragon.loan.service.AcUserBankDragonService;
import com.dragon.loan.service.LoanInfoIDragonService;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.dubbo.coreservice.controller.LoanApplyContactInfoDTO;
import com.marvels.dubbo.coreservice.dto.LoanExtInfoDTO;
import com.marvels.dubbo.coreservice.service.ILoanApplyInfoService;

@Service
public class LoanApplyInfoServiceImpl implements ILoanApplyInfoService {
	@Autowired
	private LoanInfoIDragonService loanInfoIDragonService;
	@Autowired
	private AcUserBankDragonService acUserBankDragonService;
	@Autowired
	private AcLoanApplyInfoExtDragonService acLoanApplyInfoExtDragonService;
	@Autowired
	private AcLoanApplyNodeService acLoanApplyNodeService;

	@Override
	public McamLoanApplyInfo findLoanApplyInfo(String loanId) throws Exception {
		// TODO Auto-generated method stub
		MarvelsLogUtil.getInstance().info("调用入参findLoanApplyInfo======================" + loanId);
		McamLoanApplyInfo applyInfo = loanInfoIDragonService.synchronizeLoanApplyInfo(loanId);
		MarvelsLogUtil.getInstance().info(loanId + "==========调用返回参数============" + applyInfo);
		return applyInfo;
	}

	@Override
	public List<McamAcLoanApplyBdInfo> findLoanApplyBdInfo(String loanId) throws Exception {
		MarvelsLogUtil.getInstance().info("调用入参findLoanApplyBdInfo======================" + loanId);
		List<McamAcLoanApplyBdInfo> list = loanInfoIDragonService.synchronizeLoanApplyBxInfo(loanId);
		MarvelsLogUtil.getInstance().info(loanId + "==========调用返回参数============" + list);
		return list;
	}

	@Override
	public List<McamAcLoanApplyImageInfo> findLoanApplyImageInfo(String loanId) throws Exception {
		MarvelsLogUtil.getInstance().info("调用入参findLoanApplyImageInfo======================" + loanId);
		List<McamAcLoanApplyImageInfo> list = loanInfoIDragonService.synchronizeLoanApplyImageInfo(loanId);
		MarvelsLogUtil.getInstance().info(loanId + "==========调用返回参数============" + list);
		return list;
	}

	@Override
	public List<LoanApplyContactInfoDTO> queryLinkmanList(String loanId) {
		MarvelsLogUtil.getInstance().info("调用入参queryLinkmanList======================" + loanId);
		List<AcLoanApplyLinkman> linkedManList = loanInfoIDragonService.queryLinkmanList(loanId);
		List<LoanApplyContactInfoDTO> contacts = new ArrayList<LoanApplyContactInfoDTO>();
		if(CollectionUtils.isNotEmpty(linkedManList)){
			for (AcLoanApplyLinkman dto : linkedManList) {
				LoanApplyContactInfoDTO contact = new LoanApplyContactInfoDTO();
				contact.setLoanId(dto.getLoanId());
				contact.setContactName(dto.getLinkmanName());
				contact.setContactMobile(dto.getLinkmanMobile());
				if(!StringUtils.isEmpty(dto.getIsEmergencyLinkman())){
					contact.setIsEmergencyContact(Integer.parseInt(dto.getIsEmergencyLinkman()));
				}
				contact.setRelationCode(dto.getRelationId());
				contact.setRelationName(dto.getRelationName());
				contacts.add(contact);
				
			}
		}
		return contacts;
	}

	@Override
	public Map<String, Object> queryLoanInfoByCondition(Map<String, Object> map) {
		MarvelsLogUtil.getInstance().info("调用入参queryLoanInfoByCondition======================" + map);
		return loanInfoIDragonService.queryLoanInfoByCondition(map);
	}

	@Override
	public List<AcUserBank> selectUserBankInfo(Map<String,Object> paramMap) {
		MarvelsLogUtil.getInstance().info("调用入参selectUserBankInfo======================" + paramMap);
		if(paramMap==null) return null;
		AcUserBank user = new AcUserBank();
		Integer id = (Integer) paramMap.get("id");
		String idCard = (String) paramMap.get("idCard");
		if(id!=null&&id!=0){
			user.setId(id);
		}
		if(!StringUtils.isEmpty(idCard)){
			user.setIdCard(idCard);
		}
		return acUserBankDragonService.selectUserBankInfo(user);
	}

	@Override
	public LoanExtInfoDTO queryExtInfoByLoanId(String loanId) {
		MarvelsLogUtil.getInstance().info("调用入参findLoanApplyImageInfo======================" + loanId);
		Map<String, String> data = acLoanApplyInfoExtDragonService.queryExtInfoByLoanId(loanId);
		MarvelsLogUtil.getInstance().info(loanId + "==========调用返回参数============" + JSONObject.toJSONString(data));
		LoanExtInfoDTO loanExtInfoDTO = new LoanExtInfoDTO();
		if(data != null){
			loanExtInfoDTO.setZmsCore(data.get("zms_core"));
			loanExtInfoDTO.setAlipayAuth(data.get("alipay_auth"));
			loanExtInfoDTO.setOperatorAuth(data.get("operator_auth"));
			loanExtInfoDTO.setHuabaiLimit(data.get("huabai_limit"));
			loanExtInfoDTO.setIsFace(data.get("is_face"));
			loanExtInfoDTO.setIsVerify(data.get("is_verify"));
		}
		return loanExtInfoDTO;
	}

	@Override
	public void updateLoanMoneyAct(String loanId, String loanMoneyAct) {
		MarvelsLogUtil.getInstance().info("调用入参updateLoanMoneyAct======================" + loanId);
		AcLoanApplyInfo acLoanApplyInfo = new AcLoanApplyInfo();
		acLoanApplyInfo.setLoanMoneyAct(new BigDecimal(loanMoneyAct));
		acLoanApplyInfo.setLoanId(loanId);
		loanInfoIDragonService.input(acLoanApplyInfo);
	}

	@Override
	public void loanNodeFlow(String loanId, String result) {
		if("yes".equals(result)){
			acLoanApplyNodeService.insertSelective(loanId, ApplyNode.REVIEW_SUCCESS);
			MarvelsLogUtil.getInstance().info("loanId:" + loanId + "，审批通过~");
		}else if("no".equals(result)){
			acLoanApplyNodeService.insertSelective(loanId, ApplyNode.REVIEW_REFUSE);
			MarvelsLogUtil.getInstance().info("loanId:" + loanId + "，审批拒绝~");
		}
	}
}
